import { obtainPubkey, login, logout, getInfo, ResetPwd, VerificationCode, registered } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    user_state: null,
    pub_key: '',
    acc_key: '',
    expire_time: '',
    name: 'cnsellers',
    avatar: '',
    roles: [],
    reset_account: ''
  },

  mutations: {
    SET_PUBKEY: (state, encryption) => {
      state.pub_key = encryption.pub_key
      state.acc_key = encryption.acc_key
      state.expire_time = encryption.expire_time
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USERSTATE: (state, user_state) => {
      state.user_state = user_state
      console.log('state store', state.user_state)
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    RESET_ACCOUNT: (state, reset_account) => {
      state.reset_account = reset_account
    }
  },

  actions: {
    // 获取加密公钥
    ObtainPubkey({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const usernameBase64 = btoa(username)
      return new Promise((resolve, reject) => {
        obtainPubkey(usernameBase64, userInfo.timestamp, userInfo.logintype).then(response => {
          const data = response.data
          commit('SET_PUBKEY', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo.acc_key, userInfo.passwordEncrypt).then(response => {
          const data = response.data
          const user_state = data.service_info_status
          setToken(data.token)
          commit('SET_TOKEN', data.token)
          commit('SET_USERSTATE', user_state)
          resolve(user_state)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          const data = response.data
          commit('SET_NAME', data.user_nickname)
          commit('SET_AVATAR', data.avatar)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },
    // 进行重置密码
    ResetPassword({ commit, data }) {
      return new Promise((resolve, reject) => {
        ResetPwd(data).then(response => {
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 验证邮件的正确
    VerificationToCode({ commit }, senddata) {
      return new Promise((resolve, reject) => {
        VerificationCode(btoa(senddata.account), senddata.msg_type, senddata.code).then(response => {
          commit('RESET_ACCOUNT', btoa(senddata.account))
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 注册
    Registered({ commit }, sendData) {
      return new Promise((resolve, reject) => {
        registered(btoa(sendData.account), sendData.password, sendData.repassword, sendData.countrycode, sendData.code).then(response => {
          const data = response.data
          setToken(data.token)
          commit('SET_TOKEN', data.token)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
